﻿using System.Collections.Generic;
using System.Linq;
using mki.Backup.Objects;
using mki.Backup.Objects.Helper;
using mki.Backup.Objects.ValidationObjects;

namespace mki.Backup.GUI.Helper
{
	/// <summary>
	/// Represents the BackupConfigurationExtension class
	/// </summary>
	internal static class BackupConfigurationExtension
	{
		#region Methods (1)

		// Public Static Methods (1) 

		/// <summary>
		/// Validates the specified source.
		/// </summary>
		/// <param name="source">The source.</param>
		/// <returns>Returns if current config is valid</returns>
		/// <exception cref="ValidationException">Throws ValidationException</exception>
		public static void Validate(this BackupConfiguration source)
		{
			var validationErrors = new List<ValidationError>();

			foreach (var backupJobElement in source.BackupJobsCollection.Cast<BackupJobElement>())
			{
				// ToDo: make me more generic
				var errorObject = backupJobElement.Validate();
				if (errorObject != null) { validationErrors.Add(errorObject); }

				// db backup jobs
				foreach (var element in backupJobElement.DBBackupJobsCollection.Cast<DBBackupElement>())
				{
					errorObject = element.Validate();
					if (errorObject != null) { validationErrors.Add(errorObject); }
				}

				// process jobs
				foreach (var element in backupJobElement.RunProcessJobsCollection.Cast<RunProcessElement>())
				{
					errorObject = element.Validate();
					if (errorObject != null) { validationErrors.Add(errorObject); }
				}

				// ftp jobs
				foreach (var element in backupJobElement.FTPJobsCollection.Cast<FTPJobElement>())
				{
					errorObject = element.Validate();
					if (errorObject != null) { validationErrors.Add(errorObject); }

					foreach (var subElement in element.AddFileCollection.Cast<FTPAddElement>())
					{
						errorObject = subElement.Validate();
						if (errorObject != null) { validationErrors.Add(errorObject); }
					}

					foreach (var subElement in element.DeleteFileCollection.Cast<FTPDeleteElement>())
					{
						errorObject = subElement.Validate();
						if (errorObject != null) { validationErrors.Add(errorObject); }
					}
				}

				// zip jobs
				foreach (var element in backupJobElement.ZipJobsCollection.Cast<ZipJobElement>())
				{
					errorObject = element.Validate();
					if (errorObject != null) { validationErrors.Add(errorObject); }

					foreach (var subElement in element.AddFileCollection.Cast<ZipAddFileElement>())
					{
						errorObject = subElement.Validate();
						if (errorObject != null) { validationErrors.Add(errorObject); }
					}

					foreach (var subElement in element.AddFolderCollection.Cast<ZipAddFolderElement>())
					{
						errorObject = subElement.Validate();
						if (errorObject != null) { validationErrors.Add(errorObject); }
					}
				}
			}

			// got errors? throw them
			if (validationErrors.Count > 0)
			{
				var validationException = new ValidationException();
				validationException.Data.Add("errors", validationErrors);
				throw validationException;
			}
		}

		#endregion Methods
	}
}